package com.example.sunmiv1.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.member.MemberAnalysisInfo;
import com.example.sunmiv1.views.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MemberUserAdapter extends BaseAdapter {

    private List<MemberAnalysisInfo.DataBean.ListBean> listBeans;
    private ViewHolder viewHolder;

    public MemberUserAdapter(List<MemberAnalysisInfo.DataBean.ListBean> listBeans) {
        this.listBeans = listBeans;
    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_member_user, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MemberAnalysisInfo.DataBean.ListBean listBean = listBeans.get(position);
        RequestOptions options = new RequestOptions();
        options.override(300, 300);
        options.placeholder(R.mipmap.default_img);//佔位圖  .error(R.mipmap.error_food)//如果是錯誤的情況
        Glide.with(parent.getContext())
                .load(listBean.getHeadimgurl())
                .apply(options)
                .into(viewHolder.memberUserIcon);
        viewHolder.memberUserName.setText(listBean.getName() + "|" + listBean.getNickname());
        viewHolder.memberUserMoney.setText(parent.getContext().getString(R.string.collect_money) + listBean.getExpense_amount());
        viewHolder.memberUserNum.setText(parent.getContext().getString(R.string.collect_money) + listBean.getExpense_num());
        viewHolder.memberUserTime.setText(parent.getContext().getString(R.string.collect_money) + listBean.getExpense_last());
        return convertView;
    }

    public void setClick(ClickCallback clickCallback) {
        this.clickCallback = clickCallback;
    }

    private ClickCallback clickCallback;

    public interface ClickCallback {
        void success(int position);
    }

    static class ViewHolder {
        @BindView(R.id.memberUser_icon)
        CircleImageView memberUserIcon;
        @BindView(R.id.memberUser_name)
        TextView memberUserName;
        @BindView(R.id.memberUser_num)
        TextView memberUserNum;
        @BindView(R.id.memberUser_time)
        TextView memberUserTime;
        @BindView(R.id.memberUser_money)
        TextView memberUserMoney;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
